﻿using System;
using System.Collections.Generic;
using System.Linq;
using TaleWorlds.Library;
using TaleWorlds.SaveSystem;

namespace TaleWorlds.CampaignSystem
{
	// Token: 0x0200002D RID: 45
	internal class CampaignBehaviorDataStore
	{
		// Token: 0x060002CE RID: 718 RVA: 0x00012510 File Offset: 0x00010710
		internal CampaignBehaviorDataStore()
		{
			this._behaviorDict = new Dictionary<string, CampaignBehaviorDataStore.BehaviorSaveData>();
		}

		// Token: 0x060002CF RID: 719 RVA: 0x00012524 File Offset: 0x00010724
		internal void SaveBehaviorData(CampaignBehaviorBase campaignBehavior)
		{
			string stringId = campaignBehavior.StringId;
			CampaignBehaviorDataStore.BehaviorSaveData behaviorSaveData = new CampaignBehaviorDataStore.BehaviorSaveData(true);
			campaignBehavior.SyncData(behaviorSaveData);
			if (this._behaviorDict.ContainsKey(stringId))
			{
				Debug.FailedAssert("trying to save multiple behaviors with the same stringid: " + stringId, "C:\\Develop\\MB3\\Source\\Bannerlord\\TaleWorlds.CampaignSystem\\CampaignBehaviorDataStore.cs", "SaveBehaviorData", 75);
				this._behaviorDict[stringId] = behaviorSaveData;
				return;
			}
			this._behaviorDict.Add(stringId, behaviorSaveData);
		}

		// Token: 0x060002D0 RID: 720 RVA: 0x0001258B File Offset: 0x0001078B
		internal void ClearBehaviorData()
		{
			this._behaviorDict.Clear();
		}

		// Token: 0x060002D1 RID: 721 RVA: 0x00012598 File Offset: 0x00010798
		internal void LoadBehaviorData(CampaignBehaviorBase campaignBehavior)
		{
			string stringId = campaignBehavior.StringId;
			CampaignBehaviorDataStore.BehaviorSaveData dataStore;
			if (this._behaviorDict.TryGetValue(stringId, out dataStore))
			{
				campaignBehavior.SyncData(dataStore);
				return;
			}
			List<KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData>> list = this._behaviorDict.ToList<KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData>>();
			string name = campaignBehavior.GetType().Name;
			foreach (KeyValuePair<string, CampaignBehaviorDataStore.BehaviorSaveData> keyValuePair in list)
			{
				if (keyValuePair.Key.Contains(name))
				{
					this._behaviorDict.Remove(keyValuePair.Key);
					this._behaviorDict.Add(stringId, keyValuePair.Value);
					campaignBehavior.SyncData(keyValuePair.Value);
					break;
				}
			}
		}

		// Token: 0x060002D2 RID: 722 RVA: 0x0001265C File Offset: 0x0001085C
		internal static void AutoGeneratedStaticCollectObjectsCampaignBehaviorDataStore(object o, List<object> collectedObjects)
		{
			((CampaignBehaviorDataStore)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
		}

		// Token: 0x060002D3 RID: 723 RVA: 0x0001266A File Offset: 0x0001086A
		protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
		{
			collectedObjects.Add(this._behaviorDict);
		}

		// Token: 0x060002D4 RID: 724 RVA: 0x00012678 File Offset: 0x00010878
		internal static object AutoGeneratedGetMemberValue_behaviorDict(object o)
		{
			return ((CampaignBehaviorDataStore)o)._behaviorDict;
		}

		// Token: 0x040000D8 RID: 216
		[SaveableField(1)]
		private readonly Dictionary<string, CampaignBehaviorDataStore.BehaviorSaveData> _behaviorDict;

		// Token: 0x0200047F RID: 1151
		internal class BehaviorSaveData : IDataStore
		{
			// Token: 0x060041A1 RID: 16801 RVA: 0x00142C47 File Offset: 0x00140E47
			public BehaviorSaveData(bool isSaving)
			{
				this._isSaving = isSaving;
			}

			// Token: 0x060041A2 RID: 16802 RVA: 0x00142C64 File Offset: 0x00140E64
			public bool SyncData<T>(string key, ref T data)
			{
				if (this.IsSaving)
				{
					this._records.Add(key, data);
					return true;
				}
				object obj;
				if (this._records.TryGetValue(key, out obj))
				{
					data = (T)((object)obj);
					return true;
				}
				return false;
			}

			// Token: 0x17000D63 RID: 3427
			// (get) Token: 0x060041A3 RID: 16803 RVA: 0x00142CB1 File Offset: 0x00140EB1
			public bool IsSaving
			{
				get
				{
					return this._isSaving;
				}
			}

			// Token: 0x17000D64 RID: 3428
			// (get) Token: 0x060041A4 RID: 16804 RVA: 0x00142CB9 File Offset: 0x00140EB9
			public bool IsLoading
			{
				get
				{
					return !this._isSaving;
				}
			}

			// Token: 0x060041A5 RID: 16805 RVA: 0x00142CC4 File Offset: 0x00140EC4
			internal static void AutoGeneratedStaticCollectObjectsBehaviorSaveData(object o, List<object> collectedObjects)
			{
				((CampaignBehaviorDataStore.BehaviorSaveData)o).AutoGeneratedInstanceCollectObjects(collectedObjects);
			}

			// Token: 0x060041A6 RID: 16806 RVA: 0x00142CD2 File Offset: 0x00140ED2
			protected virtual void AutoGeneratedInstanceCollectObjects(List<object> collectedObjects)
			{
				collectedObjects.Add(this._records);
			}

			// Token: 0x060041A7 RID: 16807 RVA: 0x00142CE0 File Offset: 0x00140EE0
			internal static object AutoGeneratedGetMemberValue_records(object o)
			{
				return ((CampaignBehaviorDataStore.BehaviorSaveData)o)._records;
			}

			// Token: 0x0400138E RID: 5006
			[SaveableField(0)]
			private Dictionary<string, object> _records = new Dictionary<string, object>();

			// Token: 0x0400138F RID: 5007
			private readonly bool _isSaving;
		}
	}
}
